package com.woniuxy.movice.handler;

import java.text.SimpleDateFormat;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.woniuxy.movice.dto.Page;
import com.woniuxy.movice.dto.Result;
import com.woniuxy.movice.entity.Orders;
import com.woniuxy.movice.entity.Users;
import com.woniuxy.movice.exception.OrderException;
import com.woniuxy.movice.pojo.Order;
import com.woniuxy.movice.service.OrderService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 * 用户订单相关
 * 
 * @author lxq
 * @data 2019年9月11日下午7:36:14
 */
@Api("订单接口")
@Controller
@RequestMapping("order")
public class OrderHandler {

	@Autowired
	private OrderService orderService;

	/**
	 * 创建订单
	 * 
	 * @param roundId
	 * @param selectSets
	 * @param request
	 * @return
	 */
	@ApiOperation(value = "创建订单", notes = "场次ID:roundId,选座selectSets：'[[1,2],[1,3]'")
	@RequestMapping(value = "creatOrder", method = RequestMethod.POST)
	public ModelAndView creatOrder(Integer roundId, String selectSets, HttpSession session) throws OrderException {
		Users user = (Users) session.getAttribute(session.getId());
		Orders order = orderService.creatOrder(roundId, selectSets, user);
		return this.showOrderInfo(session, order.getOid());

	}

	/**
	 * 查询用户的订单
	 * 
	 * @param state
	 *            订单状态，为空即查询所有
	 * @param session
	 * @param page
	 *            起始页
	 * @param size
	 *            每页多少条
	 * @return
	 */
	@ApiOperation(value = "根据订单状态查询对应订单", notes = "订单状态 state:0,1,2,3,4,5 可不填'")
	@RequestMapping(value = "listAll", method = RequestMethod.GET)
	@ResponseBody
	public Result findAllOrder(@RequestParam(value = "state", required = false) Integer state,
			@RequestParam(value = "page", defaultValue = "0") Integer page,
			@RequestParam(value = "size", defaultValue = "10") Integer size, HttpSession session) {
		Users user = (Users) session.getAttribute(session.getId());
		Page<Order> orders = orderService.finAllOrder(user.getUid(), page, size, state);
		Result result = new Result();
		result.setFlag(true);
		result.setData(orders);
		result.setCode(Result.OK);
		return result;
	}
	@ApiOperation(value = "查询订单详细")
	@RequestMapping(value="showOrder",method=RequestMethod.GET)
	public ModelAndView showOrderInfo(HttpSession session, Integer id) {
		Users user = (Users) session.getAttribute(session.getId());
		Order order=orderService.findOrderByID(id);
		if(!order.getUser().getUid().equals(user.getUid())){
			throw new OrderException("账号异常，请使用订单对应的用户进行查询");
		}
		ModelAndView modelAndView = new ModelAndView();
		long maxtime = ((order.getOtime().getTime() + (60 * 30*1000)) - System.currentTimeMillis())/1000;
		SimpleDateFormat dtf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm");
		String startTime = dtf.format(order.getRound().getPstart());
		modelAndView.addObject("order", order);
		modelAndView.addObject("maxtime", maxtime);
		modelAndView.addObject("startTime", startTime);
		modelAndView.setViewName("order");
		return modelAndView;
	}
	
	@ApiOperation(value = "取票")
	@RequestMapping(value="takeTicket",method=RequestMethod.POST)
	@ResponseBody
	public Result takeTicket(String account,String orderId){
		Result result = new Result();
		try{
			boolean flag=orderService.takeTicket(account,orderId);
			result.setFlag(flag);
			result.setData("取票成功");
			result.setCode(Result.OK);
		}catch(OrderException e){
			result.setFlag(false);
			result.setCode(Result.ERROR);
			result.setData(e.getMessage());
		}
		return result;
	}
}
